﻿package de.gameduell.framework.facebook.app.pyramide {
	import de.gameduell.framework.facebook.app.Controller;
	import de.gameduell.framework.facebook.app.GameData;

	import de.gameduell.framework.debug.Debug;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;

	public class Brick extends EventDispatcher {
		public static const STATE_UNDEFINED:String = "STATE_UNDEFINED";
		public static const STATE_CONSTRUCTION:String = "STATE_CONSTRUCTION";
		public static const STATE_SELECT:String = "STATE_SELECT";
		public static const STATE_COMPLETED:String = "STATE_COMPLETED";

		public static const BRICK_CHANGED:String = "BRICK_UPDATED";
		public static const BRICK_STATE_CHANGED:String = "BRICK_STATE_CHANGED";
		public static const BRICK_TYPE_CHANGED:String = "BRICK_TYPE_CHANGED";
		public static const BRICK_SCORE_CHANGED:String = "BRICK_SCORE_CHANGED";
		public static const BRICK_BUSY:String = "BRICK_BUSY";
		
		public static const TYPE_UNDEFINED:String = "TYPE_UNDEFINED";
		public static const TYPE_CONSTRUCTION:String = "TYPE_CONSTRUCTION";
		public static const TYPE_COMPLETED:String = "TYPE_COMPLETED";

		private var _id:String;
		private var _coins:int;
		private var _coinsNeeded:int;
		private var _state:String;
		private var _isOnTop:Boolean;
		private var _brickType:String;
		private var controller:Controller;
		private var _position:Point;
		private var _score:int;
		private var _maxLives:int;
		private var _brickTypesAvailable:Array;
		private var _rowId:int;

		public function Brick(aXML:XML) {
			_brickTypesAvailable = new Array();
			_id = String(aXML.@id);
			_coinsNeeded = int(aXML.@coinsNeeded);
			parseAvailableBricks(aXML.@bricks);
			_maxLives = int(aXML.@maxLives);
			_state = STATE_UNDEFINED;
			_brickType = TYPE_UNDEFINED;
			controller = Controller.getInstance();
		}
		
		public function get rowId():int {
			return _rowId;
		}
		
		public function set rowId(anID:int):void {
			_rowId = anID;
		}
		
		private function parseAvailableBricks(aBrickString:String):void {
			var splitter:Array = aBrickString.split(",");
			if(splitter.length > 1){
				for(var i:int = 0; i < splitter.length; i++){
					_brickTypesAvailable.push(splitter[i]);
				}
			}
		}

		public function get brickTypesAvailable():Array {
			return _brickTypesAvailable;
		}

		public function get id():String {
			return _id;
		}

		public function get maxLives():int {
			return _maxLives;
		}

		public function get score():int {
			return _score;
		}

		public function get coins():int {
			return _coins;
		}

		public function get coinsNeeded():int {
			return _coinsNeeded;
		}

		public function get isOnTop():Boolean {
			return _isOnTop;
		}

		public function set isOnTop(onTop:Boolean):void {
			_isOnTop = onTop;
		}

		public function set score(aScore:int):void {
			if(aScore > _score){
				Debug.trace("Brick::set score for brick ID: " + _id + " score: " + aScore);
				_score = aScore;
				dispatchEvent(new Event(BRICK_SCORE_CHANGED));
			}
		}

		public function set position(aPos:Point):void {
			_position = aPos;
		}

		public function get position():Point {
			return _position;
		}

		public function get brickType():String {
			return _brickType;
		}

		public function set brickType(aType:String):void {
			if(_brickType != aType){
				_brickType = aType;
				dispatchEvent(new Event(BRICK_TYPE_CHANGED));
			}
		}

		public function get state():String {
			return _state;
		}

		public function set state(aState:String):void {
			_state = aState;
			if(_state == STATE_CONSTRUCTION){
				_brickType = "brick_const_1";
				controller.gameData.addEventListener(GameData.APP_COINS_CHANGED, checkCoins);
			}
			dispatchEvent(new Event(BRICK_STATE_CHANGED));
		}

		private function checkCoins(event:Event):void {
			dispatchEvent(new Event(BRICK_CHANGED));
			if(controller.gameData.coins >= coinsNeeded) {
				controller.gameData.removeEventListener(GameData.APP_COINS_CHANGED, checkCoins);
				state = STATE_SELECT;
			}
		}

		public function update(newData:String):void {
			// TODO - update data from incomming string ...
			dispatchEvent(new Event(BRICK_CHANGED));
		}
		
		override public function toString():String {
			return "Brick: id=" + _id + " state=" + _state + " type=" + _brickType;
		}
		
		public function destroy():void {
			Debug.trace("Brick::destroy", Debug.ERROR);
			controller.gameData.removeEventListener(GameData.APP_COINS_CHANGED, checkCoins);
			_brickTypesAvailable = null;
			_id = null;
			_coinsNeeded = 0;
			_maxLives = 0;
			_state = null;
			_brickType = null;
			controller = null;
			_state = null;
			_brickType = null;
		}
	}
}